package com.goods.controller.business;


import com.goods.business.service.ProductCategoryService;
import com.goods.common.annotation.ControllerEndpoint;
import com.goods.common.error.SystemException;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.system.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * projectName: goods
 *
 * @author: ysc
 * time: 2022/12/2 10:58 周五
 * description:
 */
@RestController
@Api(tags = "业务管理-物资类别")
@RequestMapping("/business/productCategory")
public class ProductCategoryController {

    @Autowired
    private ProductCategoryService productService;



    //获取物资类别列表
    @ApiOperation(value = "物资列表", notes = "物资列表,根据物资名模糊查询")
    @GetMapping("/categoryTree")
    public ResponseBean<PageVO<ProductCategoryTreeNodeVO>> findcategoryTree(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                                               @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize){
        PageVO<ProductCategoryTreeNodeVO> productList = productService.findcategoryTree(pageNum, pageSize);

        return ResponseBean.success(productList);
    }


    //获取父分类列表信息
    @ApiOperation(value = "获取父分类列表信息")
    @GetMapping("getParentCategoryTree")
    public ResponseBean getParentCategoryTree(){
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOList= productService.getParentCategoryTree();
        return ResponseBean.success(productCategoryTreeNodeVOList);
    }


    /**
     * 添加物资
     *
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "添加物资失败", operation = "添加物资")
    @RequiresPermissions({"product:add"})
    @ApiOperation(value = "添加物资")
    @PostMapping("/add")
    public ResponseBean add(@RequestBody @Validated ProductCategory productCategory) {
        productService.add(productCategory);
        return ResponseBean.success();
    }


    //编辑物资
    @ApiOperation("编辑物资")
    @RequiresPermissions({"product:edit"})
    @GetMapping("/edit/{id}")
    public ResponseBean edit(@PathVariable Long id) throws SystemException {
        ProductCategory productCategory = productService.edit(id);
        return ResponseBean.success(productCategory);
    }


    /**
     * 更新物资
     *
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "更新物资失败", operation = "更新物资")
    @ApiOperation(value = "更新物资")
    @RequiresPermissions({"product:update"})
    @PutMapping("/update/{id}")
    public ResponseBean update(@PathVariable Long id, @RequestBody @Validated ProductCategory productCategory) throws SystemException {
        productService.update(id, productCategory);
        return ResponseBean.success();
    }


    /**
     * 删除物资
     *
     * @param id
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "删除物资失败", operation = "删除物资")
    @ApiOperation(value = "删除物资")
    @RequiresPermissions({"product:delete"})
    @DeleteMapping("/delete/{id}")
    public ResponseBean delete(@PathVariable Long id) throws SystemException {
        productService.delete(id);
        return ResponseBean.success();
    }



}
